<template>
  <div class="login-page">
    <!-- 顶部黄色区域 -->
    <div class="top-section">
      <van-icon name="arrow-left" class="back-icon" />
      <h1 class="login-title">密码登录</h1>
      <p class="login-subtitle">登录帐号发现更多精彩</p>
      <!-- 卡通小狗 -->
      <div class="dog-illustration">
        <img
          src="../photo/小狗出售-copy.png"
          alt="cartoon dog"
          class="dog-img" />
      </div>
    </div>

    <!-- 表单区域 -->
    <div class="form-section">
      <!-- 用户名输入（带清除按钮） -->
      <van-field
        v-model="loginForm.username"
        label="用户名"
        placeholder="请输入用户名"
        :rules="[{ required: true, message: '请输入用户名' }]"
        class="custom-field">
        <template #button>
          <van-icon
            name="cross"
            @click="clearUsername"
            v-if="loginForm.username" />
        </template>
      </van-field>

      <!-- 密码输入（带"忘记密码"） -->
      <van-field
        v-model="loginForm.password"
        type="password"
        label="密码"
        placeholder="请输入密码"
        :rules="[{ required: true, message: '请输入密码' }]"
        class="custom-field">
        <template #button>
          <van-button size="small" type="text" class="forgot-password-btn"
            >忘记密码</van-button
          >
        </template>
      </van-field>

      <!-- 协议勾选框 -->
      <van-checkbox v-model="agree" class="agree-checkbox">
        我已阅读并同意《用户协议》和《隐私政策》
      </van-checkbox>

      <!-- 登录按钮（未满足条件时置灰） -->
      <van-button
        type="primary"
        block
        :disabled="!agree || !loginForm.username || !loginForm.password"
        @click="handleLogin"
        class="login-btn">
        登录
      </van-button>

      <!-- 验证码登录 + 新用户注册 -->
      <div class="other-options">
        <van-button type="text" class="option-btn" @click="switchToSmsLogin"
          >验证码登录</van-button
        >
        <van-button type="text" class="option-btn" @click="goToRegister"
          >新用户注册</van-button
        >
      </div>

      <!-- 第三方登录（微信） -->
      <div class="third-party">
        <div class="divider">
          <span class="divider-text">第三方登录</span>
        </div>
        <van-icon name="wechat" class="wechat-icon" @click="wechatLogin" />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue"
import { useUserStore } from "../stores/user"
import { useRouter } from "vue-router"
// 使用reactive创建响应式表单对象
const loginForm = reactive({
  username: "",
  password: "",
})
const router = useRouter()
const userStore = useUserStore()
const agree = ref(false)

// 方法：清除用户名
const clearUsername = () => {
  loginForm.username = ""
}

// 方法：登录逻辑
const handleLogin = async () => {
  console.log("触发登录：", loginForm)

  try {
    // 调用登录API，传递loginForm对象
    const response = await userStore.login(loginForm)
    await userStore.getLoginUser()
    console.log("登录响应：", response)

    // 这里可以添加登录成功后的处理逻辑
    router.push({ name: "recommend" })
    // 例如：跳转页面、存储token等
  } catch (error) {
    console.error("登录失败：", error)
    // 这里可以添加登录失败的处理逻辑
    // 例如：显示错误提示
  }
}

// 方法：切换到验证码登录
const switchToSmsLogin = () => {
  console.log("切换到验证码登录")
}

// 方法：前往新用户注册
const goToRegister = () => {
  router.push({ name: "register" })
}

// 方法：微信第三方登录
const wechatLogin = () => {
  console.log("微信登录")
}
</script>

<style scoped>
.login-page {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #fff;
}

/* 顶部黄色区域样式 */
.top-section {
  background-color: #ffd700;
  padding: 16px 20px 60px;
  position: relative;
  height: 40vh;
}

.back-icon {
  font-size: 20px;
  color: #333;
}

.login-title {
  font-size: 22px;
  font-weight: bold;
  margin: 20px 0 8px;
}

.login-subtitle {
  font-size: 14px;
  color: #666;
  margin-bottom: 20px;
}

.dog-illustration {
  position: absolute;
  bottom: -30px;
  right: 20px;
}

.dog-img {
  width: 120px;
  height: auto;
}

/* 表单区域样式 */
.form-section {
  flex: 1;
  padding: 40px 20px 20px;
  background-color: #fff;
  border-top-left-radius: 20px;
  border-top-right-radius: 20px;
  margin-top: -20px;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
}

/* 自定义输入框样式 */
.custom-field {
  padding: 12px 0;
  margin-bottom: 0;
}

.custom-field :deep(.van-field__label) {
  width: 70px;
  color: #333;
  font-size: 16px;
}

.custom-field :deep(.van-field__body) {
  border-bottom: 1px solid #eee;
  padding-bottom: 8px;
}

.custom-field :deep(.van-field__control) {
  font-size: 16px;
}

/* 忘记密码按钮 */
.forgot-password-btn {
  color: #333 !important;
  font-size: 12px;
  border: none !important;
  padding: 0 !important;
  background-color: transparent !important; /* 背景透明，与页面背景融合 */
}

/*  hover 时也保持背景透明（避免 hover 触发默认背景变化） */
.forgot-password-btn:hover,
.option-btn:hover {
  background-color: transparent !important;
}

.forgot-password-btn :deep(.van-button__text) {
  color: #333 !important;
}

/* 协议勾选样式 */
.agree-checkbox {
  margin: 20px 0;
  font-size: 14px;
  color: #666;
}

.agree-checkbox :deep(.van-checkbox__icon) {
  font-size: 18px;
}

.agree-checkbox :deep(.van-checkbox__label) {
  color: #666;
}

/* 登录按钮样式 */
.login-btn {
  border-radius: 24px;
  height: 48px;
  line-height: 48px;
  font-size: 16px;
  background: linear-gradient(to right, #ffd700, #ffaa00);
  border: none;
  margin-top: 10px;
}

.login-btn:disabled {
  background: #f5f5f5;
  color: #c8c9cc;
}

/* "验证码登录/新用户注册"的父容器布局 */
.other-options {
  display: flex;        /* 启用Flex布局 */
  justify-content: space-between; /* 子元素两端对齐 */
  margin: 20px 0;       /* 上下留间距，可根据需求调整 */
  padding: 0 10px;      /* 左右留内边距，避免按钮贴边（可选） */
}

/* 验证码登录、新用户注册按钮 */
.option-btn {
  color: #333 !important;
  font-size: 12px;
  border: none !important;
  padding: 0 !important;
  background-color: transparent !important; /* 背景透明，与页面背景融合 */
}

/* 选项按钮样式 */
.option-btn {
  color: #333 !important;
  font-size: 12px; /* 从14px调整为12px */
  border: none !important;
  padding: 0 !important;
}

.option-btn :deep(.van-button__text) {
  color: #333 !important;
}

/* 第三方登录样式 */
.third-party {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 30px;
}

.divider {
  position: relative;
  width: 100%;
  text-align: center;
  margin-bottom: 20px;
}

.divider:before {
  content: "";
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 1px;
  background-color: #eee;
}

.divider-text {
  background-color: #fff;
  padding: 0 15px;
  color: #999;
  font-size: 14px;
  position: relative;
}

.wechat-icon {
  font-size: 40px;
  color: #07c160;
}
</style>
